package com.example.administrator.zhbj;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.zhbj.utils.PrefUtils;

import java.util.ArrayList;

/**
 * 引导页
 * Created by Administrator on 2016/8/15 0015.
 */
public class GuideActivity extends Activity {

    private ViewPager vpGuide;
    private static final int [] mImageIds = new int[] {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};
    private  ArrayList <ImageView> mImageViewList;
    private LinearLayout llPointGroup;
    private int mPointWidth;//两个远点之间的距离
    private View viewRedPoint;//小红点
    private Button btnStart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.activity_guide);

        vpGuide = (ViewPager)findViewById(R.id.vp_guide);
        llPointGroup = (LinearLayout)findViewById(R.id.ll_point_group);//引导圆点的父控件
        viewRedPoint = findViewById(R.id.view_red_point);
        btnStart = (Button)findViewById(R.id.btn_start);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更新sp,表示已经展示了新手引导
                PrefUtils.setBoolean(GuideActivity.this, "is_user_guide_show", true);

                //跳转主页面
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                finish();
            }
        });

        initViews();
        vpGuide.setAdapter(new GuideAdapter());
        vpGuide.setOnPageChangeListener(new GuidePageListener());

    }

    /**
     * 初始化界面
     */
    private void initViews(){
       mImageViewList = new ArrayList<ImageView>();

        //初始化引导页的页面
        for (int i=0 ; i<mImageIds.length; i++){
            ImageView image = new ImageView(this);
            image.setBackgroundResource(mImageIds[i]);//设置引导页背景
            mImageViewList.add(image);
        }

        //初始化引导页的小圆点
        for (int i=0 ; i<mImageIds.length; i++){
            View point = new View(this);
            point.setBackgroundResource(R.drawable.shape_point_gray);//设置引导页背景

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            if (i>0){
                params.leftMargin = 10;//设置圆点间隔
            }
            point.setLayoutParams(params);//设置引导远点的大小

            llPointGroup.addView(point);//将圆点添加给线性布局
        }

        //measure测量大小  layout界面位置  ondrow显示方式
        //int width = llPointGroup.getChildAt(1).getLeft() - llPointGroup.getChildAt(0).getLeft();

        //获取视图树，对layout结束事件监听
        llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            //当layout执行结束回调此方法
            @Override
            public void onGlobalLayout() {
                llPointGroup.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //measure测量大小  layout界面位置  ondrow显示方式
                mPointWidth = llPointGroup.getChildAt(1).getLeft() - llPointGroup.getChildAt(0).getLeft();
            }
        });

    }

    /**
     * ViewPager数据适配器
     */
    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(mImageViewList.get(position));
            return mImageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }
    }

    /**
     * viewPageer的滑动监听
     */
    class GuidePageListener implements ViewPager.OnPageChangeListener{

        //滑动事件
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int len = (int)(mPointWidth * (position + positionOffset));
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewRedPoint.getLayoutParams();//获取当前红点的布局参数
            params.leftMargin = len;
            viewRedPoint.setLayoutParams(params);
        }
        //某一个页面被选中
        @Override
        public void onPageSelected(int position) {
            if (position == mImageViewList.size() - 1){//最后一个页面
                btnStart.setVisibility(View.VISIBLE);//显示按钮
            }else{
                btnStart.setVisibility(View.INVISIBLE);//隐藏按钮
            }
        }
        //滑动状态变化
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
